﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 药品信息管理
{
    public partial class YP_RKDCX : Form
    {
        public YP_RKDCX()
        {
            InitializeComponent();
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is Button) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {

            db db1 = new db();
            string time1 = dateTimePicker1.Value.ToString("yyyy.MM.dd"), time2 = dateTimePicker2.Value.ToString("yyyy.MM.dd");
            string string_rkdh1 = "", string_rkdh2 = "", string_comboBox1 = "", string_comboBox2 = "",string_rklx = " and CR.MC in ('') ", string_flag = " 状态 in ('') ";//筛选附加的sql语句
            if (textBox1.Text != "") { string_rkdh1 = " and R0.rkdh>=" + textBox1.Text + " "; }
            if (textBox2.Text != "") { string_rkdh2 = " and R0.rkdh<=" + textBox2.Text + " "; }
            if (comboBox1.SelectedText != "") { string_comboBox1 = " and GYS.MC='" + comboBox1.SelectedText + "' "; }
            if (comboBox2.SelectedText != "") { string_comboBox2 = " and GYS.MC='" + comboBox2.SelectedText + "' "; }
            //if (comboBox1.SelectedText != "") { string_comboBox1 = " and R0.gys =(select bm from YK_ZDGYS where mc='" + comboBox1.SelectedText + "') "; }


            foreach (Control aa in panelEx2.Controls)//入库类型查询初始条件
            {
                CheckBox cb = (CheckBox)aa;
                if (cb.Checked == true)
                {
                    string_rklx = string_rklx.Replace("''", "'','" + cb.Text + "'");
                }

            }


            foreach (Control aa in groupBox1.Controls)//入库单记账状态栏初始查询条件
            {
                CheckBox cb = (CheckBox)aa;
                if (cb.Checked == true)
                {
                    string_flag = string_flag.Replace("''", "'','" + cb.Text + "'");
                }

            }

            string str=@"SELECT R0.XH 序号,R0.RKDH 入库单号,convert(datetime,R0.RQ-2) 制单日期,CR.MC 入库类型,GYS.MC 供应商,
                         HJ.JJJE 进价金额,HJ.LSJE 零售金额,R0.LRRY 录入人员,R0.SHRY 审核人员,R0.SHRY 记账人员,
                        (case when (R0.FLAG &2=2) then '已记账' when (R0.FLAG &1=1) then '已审核' else '未审核' end )  状态
                        FROM YK_RKD0 R0 LEFT JOIN YK_CRLX CR ON R0.RKLX = CR.BM
                                     LEFT JOIN(SELECT RKDH,SUM(JJJE) AS JJJE,SUM(LJJE) AS LSJE FROM YK_RKD1 GROUP BY RKDH) HJ ON R0.RKDH=HJ.RKDH
                                     LEFT JOIN YK_ZDGYS GYS ON R0.GYSBM=GYS.BM
                        WHERE  ((R0.FLAG & 3 = 0) OR (R0.FLAG & 3 = 1) OR (R0.FLAG & 2 > 0)) AND R0.RKLX IN (-1,0,1,2,3,4,6,7)  
                               AND R0.RQ >= " + db1.datetoint(time1) + " AND R0.RQ <= " + db1.datetoint(time2) + string_rkdh1 + string_rkdh2 + string_comboBox1 + string_comboBox2 + string_rklx;
          str = "select * from ("+str+") a where "+string_flag+"order by 入库单号";
          dataGridView1.DataSource = db1.sqldt(str);
        }


        private void YP_RKDCX_Load(object sender, EventArgs e)
        {

            #region 显示出入库类型
            int y = 20;
            db db1 = new db();//调用连接类db
            DataTable dt = db1.sqldt("select  mc from yk_crlx");//根据条件返回查询数据。
            foreach (DataRow dr in dt.Rows)
            {
                CheckBox cb = new CheckBox();
                cb.Location = new Point(6, y);
                y = y + 23;
                cb.Text = dr["mc"].ToString();
                cb.Checked = true; 
                cb.Size = new Size(180, 16);
                panelEx2.Controls.Add(cb);
            }
            #endregion 


            dateTimePicker1.Value = DateTime.Now.AddDays(-(DateTime.Now.Day - 1));
            buttonItem1_Click(sender, e);


            getSqlConnection myconnection2 = new 药品信息管理.getSqlConnection();
            getSqlConnection myconnection = new 药品信息管理.getSqlConnection();
            SqlConnection conn = myconnection.GetCon(); 
            SqlConnection conn2 = myconnection2.GetCon();
            SqlCommand mycomm = new SqlCommand("select mc from zd_ks", conn);
            SqlCommand mycomm1 = new SqlCommand("select mc from YK_ZDGYS", conn2);
            SqlDataReader myread = mycomm.ExecuteReader();
            SqlDataReader myread1 = mycomm1.ExecuteReader();
            while (myread.Read())
            {
                this.comboBox2.Items.Add((string)myread[0]);

            }

            while (myread1.Read())
            {
                this.comboBox1.Items.Add((string)myread1[0]);
            }
            conn.Close();
        }

        private void textBoxItem1_InputTextChanged(object sender)
        {
            foreach (DataGridViewRow R1 in dataGridView1.Rows)
            {
                if (R1.Cells[1].Value.ToString() == textBoxItem1.Text)
                {
                    R1.Selected = true;
                }
                else { R1.Selected = false; }
            }
        }

        private void textBoxItem2_InputTextChanged(object sender)
        {
            foreach (DataGridViewRow R1 in  dataGridView1.Rows)
            {
                if (R1.Cells[0].Value.ToString() == textBoxItem2.Text)
                {
                    R1.Selected = true;
                }
                else { R1.Selected = false; }
            }
        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            db db1 = new db();
            db1.DataToExcel(dataGridView1);
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            dgVprint1.Print(dataGridView1,true);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            YF_RKDCX_MX mx = new YF_RKDCX_MX();
            mx.rkdh = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();//入库单号参数传递
            mx.ShowDialog();
            
        }
    }
}